<?php

class BookController extends Controller
{
	public function filters()
    {
        return array('accessControl');
    }
	public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array('show','index','search'),
                'users'=>array('*'),
            ),
            array('allow',
                'actions'=>array('create','delete','new','update'),
                'users'=>array('@'),
            ),
            array('deny',
                'users'=>array('*'),
            ),
        );
    }
	public function actionCreate()
	{
		$this->render('create');
	}

	public function actionDelete()
	{
		$this->render('delete');
	}

	public function actionEdit()
	{
		$this->render('edit');
	}

	/**
	 * 图书列表
	 * t=null 全部图书  t=1 图书速递 t=2 热门图书 t＝3 收藏最多 t=4 评论最多
	 */
	public function actionIndex()
	{
		$type = getQuery('t');
		if($type === null || $type==1){
			$criteria = new CDbCriteria;
			$criteria->order = 'print_datetime desc';
			$total = UrBook::model()->count($criteria);	
			$pages = new CPagination($total);
			$pages -> pageSize = 15;
			$pages->applyLimit($criteria);
			$booklist = UrBook::model()->findAll($criteria);
		}
		if($type==2){
			$criteria = new CDbCriteria;
			$criteria->select = '*,sum(desire) as desirelevel';
			$criteria->group = 'bid';
			$criteria->order = 'desirelevel desc';
			$total = UrVote::model()->count($criteria);	
			$pages = new CPagination($total);
			$pages -> pageSize = 15;
			$pages->applyLimit($criteria);
			$votes = UrVote::model()->findAll($criteria);
			$booklist=array();
			foreach($votes as $key=>$vote){
				$booklist[$key] = $vote->book;	
			}
		}
		$this->render('index',array('booklist'=>$booklist,'pages'=>$pages));
	}
	
	/**
	 * 显示图书详细页
	 */
	 public function actionShow(){
		 $id = getQuery('id');
		 $book = UrBook::model()->with('extra')->findByPK($id);
		 $this->render('show',array('book'=>$book));
		 }

	public function actionNew()
	{
		$model = new BookForm('add');
		if(isset($_POST['BookForm']))	{
			$small_pic = CUploadedFile::getInstanceByName('BookForm[small_pic]');
			$big_pic = CUploadedFile::getInstanceByName('BookForm[big_pic]');
			$model->attributes = $_POST['BookForm'];
			$model->big_pic_file = $big_pic;
			$model->small_pic_file = $small_pic;
			var_dump($model->saveBook());
			}
		$this->render('new',array('model'=>$model));
	}

	public function actionUpdate()
	{
		$this->render('update');
	}
	
	/*
	 * 针对图书名称的关键词搜索
	 * @params keywords
	 */
	 public function actionSearch(){
	 	$keywords = getPost('search_text');
	 	if($keywords!==null){
	 		$criteria = new CDbCriteria;
	 		$criteria->condition = "name like '%".trim($keywords)."%'";
	 		$criteria->order = "publish_datetime desc";
	 		$total = UrBook::model()->count($criteria);
	 		$pages = new CPagination($total);
	 		$pages->pageSize = 20;
	 		$pages->applyLimit($criteria);
	 		$booklist = UrBook::model()->findAll($criteria);
	 		$this->render('index',array('booklist'=>$booklist,'pages'=>$pages));
	 	}
	 }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}
